<?php ob_start();?>
<html>
<?php 
	$root = $_SERVER['DOCUMENT_ROOT'];
 	include($root.'/standart/head.php');
 	include($root.'/standart/dateHeader.php');
 	include($root.'/standart/checkUser.php');
?>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Start Bootstrap - SB Admin Version 2.0 Demo</title>
    <link href="../css/bootstrap.min.css" rel="stylesheet">
    <link href="../font-awesome/css/font-awesome.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="../css/jquery.cleditor.css" />
    <link href="../css/sb-admin.css" rel="stylesheet">
    <link href="../css/jquery-ui-1.10.4.css" rel="stylesheet" type="text/css" media="screen"/>
	<link href="../css/plugins/dataTables/dataTables.bootstrap.css" rel="stylesheet">
    
    <script type="text/javascript" src="../js/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="../js/ajax.js"></script>
    <script type="text/javascript">
    function validate(evt, obj) {
		var theEvent = evt || window.event;
	  	var key = theEvent.keyCode || theEvent.which;
	  	key = String.fromCharCode( key );
	  	var regex = /[0-9]|\./;
	  	if( !regex.test(key) ) {
	    	theEvent.returnValue = false;
	    	if(theEvent.preventDefault) theEvent.preventDefault();
	  	}else{
		  	var time = obj.value+key;
		  	if(time.indexOf('/') > 0){
				var sTime = time.split('/')[0];
				var sMin = '';
				var sHour = '';
				if(sTime.indexOf(':') > 0){
					sHour = sTime.split(':')[0];
					sMin = sTime.split(':')[1];
				}else{
					sHour = sTime;
				}
				var eTime = time.split('/')[1];
				var eMin = '';
				var eHour = '';
				if(eTime.indexOf(':') > 0){
					eHour = eTime.split(':')[0];
					eMin = eTime.split(':')[1];
				}else{
					eHour = eTime;
				}
				if(sMin != '' && parseInt(sMin) > 59){
					theEvent.returnValue = false;
				}
				if(sHour != '' && parseInt(sHour) > 23){
					theEvent.returnValue = false;
				}
				if(eMin != '' && parseInt(eMin) > 59){
					theEvent.returnValue = false;
				}
				if(eHour != '' && parseInt(eHour) > 23){
					theEvent.returnValue = false;
				}
				if(eHour.length == 2 && sHour.length == 2 && parseInt(eHour, 10) < parseInt(sHour, 10)){
					theEvent.returnValue = false;
				}
				if(eHour.length == 2 && eMin.length == 2 && sHour.length == 2 && sMin.length == 2 && parseInt(eHour, 10) == parseInt(sHour, 10) && parseInt(eMin, 10) < parseInt(sMin, 10)){
					theEvent.returnValue = false;
				}
				var retVal = '';
				if(theEvent.returnValue){
					if(sHour.length == 2){
						retVal = sHour+':';
					}else{
						retVal = sHour;
					}
					if(sMin.length == 2){
						retVal = retVal + sMin+'/';
					}else{
						retVal = retVal + sMin;
					}
					if(eHour.length == 2){
						retVal = retVal + eHour+':';
					}else{
						retVal = retVal + eHour;
					}
					if(eMin.length > 0){
						retVal = retVal + eMin;
					}
					obj.value = retVal;
				}
		  	}else{
		  		var sMin = '';
				var sHour = '';
				if(time.indexOf(':') > 0){
					sHour = time.split(':')[0];
					sMin = time.split(':')[1];
				}else{
					sHour = time;
				}
				if(sMin != '' && parseInt(sMin) > 59){
					theEvent.returnValue = false;
				}
				if(sHour != '' && parseInt(sHour) > 23){
					theEvent.returnValue = false;
				}
				var retVal = '';
				if(theEvent.returnValue){
					if(sHour.length == 2){
						retVal = sHour+':';
					}else{
						retVal = sHour;
					}
					if(sMin.length == 2){
						retVal = retVal + sMin+'/';
					}else{
						retVal = retVal + sMin;
					}
					obj.value = retVal;
				}
		  	}
		  	if(theEvent.preventDefault) theEvent.preventDefault();
	  	}
	}

	function numberCheck(evt){
		var theEvent = evt || window.event;
	  	var key = theEvent.keyCode || theEvent.which;
	  	key = String.fromCharCode( key );
	  	var regex = /[0-9]|\./;
	  	if( !regex.test(key) ) {
	    	theEvent.returnValue = false;
	    	if(theEvent.preventDefault) theEvent.preventDefault();
	  	}
	}

	function listProducts(){
		url = 'getEvents.php';
		ajx(url, 'fillProducts');
	}

	function fillProducts(_htm){
		$('.table-responsive').html(_htm);
		var table = $('#dataTables-example').dataTable({
            "language": {
                "url": "../css/plugins/dataTables/dataTablesTurkish.json"
            }
        } );
		$('#dataTables-example tbody').on( 'click', 'tr', function () {
	        if ( $(this).hasClass('selected') ) {
	            $(this).removeClass('selected');
	            clearForm();
	        }else {
		        if(table.$('tr.selected') != null){
	        		table.$('tr.selected').removeClass('selected');
		        }
	            $(this).addClass('selected');
	            row = $(this).find("td");
	            datex = row[2].innerText;
	            ev_date = datex.split(' ')[0];
	            ev_interval = datex.split(' ')[1];
	            $("#event_title").val(row[0].innerText);
				$("#event_id").val(row[3].innerText);
				$("#event_def").val(row[1].innerText);
				$("#event_interval").val(ev_interval);
				$("#event_date").val(ev_date);
				$("#event_image").prop('src', row[4].innerText);
				$("#btnDelHtml").prop('disabled', false);
	        }
	    } );
	}

	function clearForm(){
		$("#event_title").val('');
		$("#event_id").val('');
		$("#event_def").val('');
		$("#event_interval").val('');
		$("#event_date").val('');
		$("#btnDelHtml").prop('disabled', true);
		$("#event_picture").val('');
		$("#event_image").prop('src', '');
	}

	function onLoad(){
		parent.unloadPageX();
		<?php 
			if(isset($_GET['success'])){
				echo 'warningMessage();';
			}
		?>
		listProducts();
		$('.datepicker').datepicker();
	}

	function dellProducts(){
		url = 'setEvent.php?'
			+'event_id='+$("#event_id").val();
		url = url+'&DELETE=E'
		$('#invisContentView').html(ajx(url, ''));
		warningMessage();
		clearForm();
		listProducts();
	}

	function warningMessage() {
		pane = document.getElementById('invisContentView');
		if(pane.style.height == '0px'){
			pane.style.display = 'block';
			$("#invisContentView").animate({
			    height: "30px"
			}, 1000 );
			setTimeout(function(){warningMessage()}, 5000);
		}else{
			pane.style.display = 'block';
			$("#invisContentView").animate({
			    height: "0px"
			}, 1000 , function() {
				document.getElementById('invisContentView').style.display = 'none';
			});
		}
	}
    </script>
</head>

<body onload="onLoad()">
    <div id="wrapper">
        <div id="inner-page-wrapper">
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header">Etkinlik Ekleme, Silme, Değiştirme</h1>
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->
            <div id="invisContentView" style="display: none; height: 0px;">
            <?php 
            	if(isset($_GET['success'])){
					if($_GET['success'] == 'E'){
						echo '<span style="color:green; font-weight:bolder;">İşlem Başarıyla Gerçekleşti</span>';
					}else{
						echo '<span style="color:red; font-weight:bolder;">İşlem sırasında hata alındı </span>';
					}
 				}
            ?>
            </div>
            <div class="row">
                <div class="col-lg-6">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                        	Etkinlik Bilgileri
                        </div>
                        <div class="panel-body">
                        	<form role="form" action="setEvent.php" enctype="multipart/form-data" method="post" id="submitForm">
	                            <div class="row">
	                                <div class="col-lg-12">
	                                	<div class="form-group" >
                                            <label>Etkinlik Başlığı</label>
                                            <input class="form-control" name="event_title" id="event_title" type="text">
                                            <input type="hidden" id="event_id" name="event_id">
                                        </div>
	                                </div>
	                            </div>
								<div class="row">
		                        	<div class="col-lg-6">
		                            	<div class="form-group" >
                                            <label>Etkinlik Tarihi</label>
                                            <input type="text" class="form-control datepicker" id="event_date" name="event_date" >
                                        </div>
									</div>
		                        	<div class="col-lg-6">
		                            	<div class="form-group" >
	                                    	<label>Etkinlik Zamanı</label>
	                                        <input class="form-control" name="event_interval" id="event_interval" maxlength="11" type="text" onkeypress="validate(event, this)" placeholder="mm:hh / mm:hh">
										</div>
									</div>
								</div>
								<div class="row">
									<div class="col-lg-6">
                                        <div class="form-group">
                                            <label>Açıklama</label>
                                            <textarea class="form-control" rows="6" id="event_def" name="event_def"></textarea>
                                        </div>
                                        <div class="form-group">
											<label>Etkinlik Resmi</label>
	                                    	<input type="file" accept=".gif,.png,.jpg,.jpeg" id="event_picture" name="event_picture">
	                                    </div>
	                                </div>
									<div class="col-lg-6">
										<div class="form-group">
	                                    	<img alt="Etkinlik Resmi" src="" style="width: 200px; height: 200px;" align="right" id="event_image">
										</div>
	                                </div>
	                            </div>
								<!-- /.row (nested) -->
	                            <button type="button" id="btnGetHtml" class="btn btn-default" onclick="parent.loadPageX(); document.getElementById('submitForm').submit();">Kaydet</button>
	                            <button type="button" id="btnDelHtml" class="btn btn-default" disabled="disabled" onclick="dellProducts()">Sil</button>
                            </form>
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
                <div class="col-lg-6">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                        	Etkinlik Listesi
                        </div>
                        <div class="panel-body">
                <!-- /.col-lg-12 -->
			                <div class="table-responsive">
			                </div>
			            </div>
			       </div>
			   </div>
            </div>
            <!-- /.row -->
        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->
    <!-- Page-Level Demo Scripts - Forms - Use for reference -->
</body>
    <script type="text/javascript" src="../js/jquery-ui-1.10.4.js"></script>
    <script type="text/javascript" src="../js/plugins/dataTables/jquery.dataTables.js"></script>
    <script type="text/javascript" src="../js/plugins/dataTables/dataTables.bootstrap.js"></script>
</html>
<?php ob_end_flush(); ?>